package example.Api.KjPay;


import com.alibaba.fastjson.JSONObject;
import example.Data.CommonParameters;
import example.Data.Config;
import example.Data.Key;
import example.Util.HttpClientUtil;
import example.Util.StringUtils;
import example.Util.UtilTest;
import example.pojo.KjPayRequest;

import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
import java.util.HashMap;
import java.util.Map;

import static example.Util.StringUtils.get14Timestamp;
import static example.Util.StringUtils.getNowTime;

/**
 * 快捷签约接口
 * {"app_id":"hyw23030600035360021907331995059",
 * "sign_type":"RSA2",
 * "timestamp":"2024-07-08 14:40:57",
 * "notify_url":"http://demo.hylocal.com/custom/Pay/ReceiveHynetNotify.aspx",
 * "biz_content":"Jq+WUB9biv7pdDTrjY0xnmZY+r9b5c4MzcLXqLm9yJeMCtSQnZihapbWBvwG4Ic5j068zscbFouIfmCr7jXSyOZG4lr405eNHKGjJqlg7E0kLYKeOYfA3eaN/ir1jB74Bbuxxr07SICpJb+CO9VIv7LSMWdQO/ZqzqwCIxQVrI1uR+iS9061q17VLv8oI/HSfIbZ34jHnEzKFkriJu7XFgJqyCLHtTgI0U5BLh8RSoJ4jyhFB3251exiJ0RIK82foVGURvliI0Wn9zonMhVXOg9bFZPDaziZdPUVk9kyGwXCM5C+KGCfqKvg+U5kQrgGpZbRfVo55r93U881D9vbm1cGFyl8ohRhxVGauFPr/GGIp9iiQD6uo4m1y0m5WCU+c5p4L7W85fIzdZdnifxyYx9K+k3trYii0Zx4a/5Lojo+Z/brqMH6OnL7Y1cZoSKDJukI0FITvY1tACtkuPrcIl9QAKXGtiFSwgJlfMVoMCh8PIpp+9faENdxjzGz3RUZ7BGXhYgev4yWscmFlwarp4AD23tB7NGr+vGSlki/FxAIHlgXia5LfRofNHeeM6ollibq/o7HEZZ/ZJunF1/52UTw5p2JzelaNZ2MOeUtSoNBMejf5LEgINIjFvIi7+4APxXKn/D2v6N91jrV++GEoUw01KZL5eBETJ+pimbf70g=",
 * "charset":"utf-8",
 * "method":"heepay.pay.quick.sign.apply",
 * "version":"1.0",
 * "format":"json",
 * "sign":"Kf5VXiYskClXqoq6oZs+ET8VILFV/cTfbrUK7wCZPA/xt64U7ALgfg8n/9+XTUg16V9RTOZgzMht0D0cIrJASUhtRsRAh36hT6PWLRPAm2cXcM/52q6y2jpdOzcHFkXvE1m41NV79eFPZAbNXVFREIhDTvdyfAp3f5xdfPXsqA/M0V5oeS4zGbivT8F1g6Ke4vjJiF3XyL0UDRLmnLxfoP0hUbngiLHXJBDq5NZyDq4KyVNBgyMwZFfkEJpJFJDIJ1iP293NoVWJW8t80q7pEv4awWUKCF6Pzu3zQg/Mckun649YYGHBvVhn4O+BX2baTYk5Pcc7bCA8/5onNti0EQ=="}
 *
 * 返回结果示例：
 {
 "code": "10000",
 "msg": "调用成功",
 "trace_id": "1720421888003c0a86479103407904",
 "data": {
 "merch_id": 2190733,
 "out_trade_no": "m20240708144057896",
 "out_trade_time": "2024-07-08 14:40:57",
 "redirect_url": "http://heepay.hylocal.com/pay/API/PageSign/www/index.html#/?pre_sign_uid=sn2407081ecf0a176e244b5dae338d71f28df30f2&merch_id=2190733&timespan=638560475307110264&sign=0205934abf52c89f2bd1f59057eaa919"
 },
 "sign": "RbytJUSXc3wbCJhoOcD8iOushgJ+F2Zm0+FaKqrlVVGpgTo7K3ZHCTGEE9SIZ03WGMIqIzIBn/XmlK5J6tKFQWvX9BhEj5zh3K5UDnmBtym27Q/qhghiABtKE+cntPTajvisHWSCfrRHl7amv62tequFwQciCmKQ43u2L3TzKngfPwtvjz4NclT1WnxjcUrmozxGsPcRMjROv1wE2r4FHH12HfobciZl3cT3kZ35hcJpQZSOFP3WUEEIHGt/uuf4j6cwZ8XmzYBIIiZH7FqhPRiuy85dNXAjvMF8evsK73efjW/oxILp6MkMUvsYzq8c7SEHPAf3bCK4ufIFvT9DfQ=="
 }



 */


public class SignQuickApply {
    public static void main(String[] args) throws InvalidKeySpecException, NoSuchAlgorithmException {
        // 组装业务参数biz_content
        KjPayRequest BizRequest = new KjPayRequest();
        BizRequest.setReturn_url("http://demo.hylocal.com");
        BizRequest.setOut_trade_no(get14Timestamp());
        BizRequest.setOut_trade_time(getNowTime());
        BizRequest.setAttach("Test");

        // 组装payer_info参数
        KjPayRequest payer_info = new KjPayRequest();
        payer_info.setOut_user_id("wfwf");
        payer_info.setBank_card_no("6222021900000000001");
        payer_info.setBank_card_name("测试");
        payer_info.setId_card_no("43082219900101001X");

        BizRequest.setPayer_info(JSONObject.toJSONString(payer_info));

        // 组装scene_info参数
        KjPayRequest scene_info = new KjPayRequest();
        scene_info.setTerminal_type(3);


        BizRequest.setScene_info(JSONObject.toJSONString(scene_info));

        String bizContent = JSONObject.toJSONString(BizRequest);

        System.out.println("业务请求参数：-------------------------------->："+bizContent);
        System.out.println();

        // 对biz_content进行rsa2加密

        String biz_content = UtilTest.rsaEncrypt(Key.PUBLIC_KEY, bizContent);
        System.out.println("业务请求参数加密后：-------------------------------->："+biz_content);
        System.out.println();

        // 获取公共参数组装待签名串

        Map<String, String> signMapStr  = new HashMap<>();
        signMapStr.put("app_id", CommonParameters.app_id);
        signMapStr.put("method", Config.KjConfigPayMethod.KjSignMethod);
        signMapStr.put("timestamp", getNowTime());
        signMapStr.put("notify_url", CommonParameters.notify_url);
        signMapStr.put("biz_content", bizContent);
        signMapStr.put("charset", CommonParameters.charset);
        signMapStr.put("version", CommonParameters.version);
        signMapStr.put("sign_type", CommonParameters.sign_type);
        signMapStr.put("format", CommonParameters.format);

        String signStr = StringUtils.mapToStringSortedByKey(signMapStr);


        System.out.println("快捷签约签名串：-------------------------------->："+signStr);
        System.out.println();
        // 使用私钥对待签名串进行RSA2签名
        String sign = UtilTest.rsa2Sign(Key.PRIVATE_KEY,signStr,CommonParameters.charset);
        System.out.println("快捷签约签名结果：-------------------------------->："+sign);
        System.out.println();
        // 组装请求参数map发送POST请求
        Map<String, String> requestMap = new HashMap<>();
        requestMap.put("app_id", CommonParameters.app_id);
        requestMap.put("method", Config.KjConfigPayMethod.KjSignMethod);
        requestMap.put("timestamp", getNowTime());
        requestMap.put("notify_url", CommonParameters.notify_url);
        requestMap.put("biz_content", biz_content);
        requestMap.put("charset", CommonParameters.charset);
        requestMap.put("version", CommonParameters.version);
        requestMap.put("sign_type", CommonParameters.sign_type);
        requestMap.put("format", CommonParameters.format);
        requestMap.put("sign", sign);

        System.out.println("快捷签约请求参数：-------------------------------->："+JSONObject.toJSONString(requestMap));
        System.out.println();

        String response = HttpClientUtil.sendPostJson(Config.ConfigUrl.INTERNAL_API_URL, requestMap);
        System.out.println("快捷签约响应结果：-------------------------------->："+response);
        System.out.println();
        // 接收响应结果并解析 if msg=调用成功，输出，redirect_url为支付页面地址
        // 否则输出错误信息msg
    }
}
